﻿#include "VAOneXMLAdaptorSeaFacesSingleCurve.h"
#include "rapidxml.hpp"
#include <QFile>
#include <iostream>
#include <QDebug>
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITKAcousticsModel/FITKAbstractSEAFace.h"
#include "FITKAcousticsModel/FITKSEAFlatCurvedFace.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementFactory.h"

namespace IO
{
    QString VAOneXMLAdaptorSeaFacesSingleCurve::getNodeName()
    {
        return "SinglyCurvedFace";
    }

    Acoustics::FITKAbstractSEASubSys::SEASubSysType VAOneXMLAdaptorSeaFacesSingleCurve::getNodeType()
    {
        return Acoustics::FITKAbstractSEASubSys::SEASinglyCurvedShell;
    }

    Acoustics::FITKAbstractSEAFace *VAOneXMLAdaptorSeaFacesSingleCurve::createDataObjNode()
    {
        return new Acoustics::FITKSEASinglyCurvedFace;
    }

    QList<int> VAOneXMLAdaptorSeaFacesSingleCurve::readCornerList(rapidxml::xml_node<char>* node, Acoustics::FITKAbstractSEAFace *data)
    {
        QList<int> listId;
        if (nullptr == node || (nullptr == data))
        {
            return listId;
        }

        bool bOk = true;

        //单曲率面有corner参数
        int index = readIntAttribute(node, "corner_index1", &bOk);
        if (true != bOk)
        {
            qDebug()<<"cornerid1 read error";
            return listId;
        }

        listId<<index;

        index = readIntAttribute(node, "corner_index2", &bOk);
        if (true != bOk)
        {
            qDebug()<<"cornerid2 read error";
            return listId;
        }
        listId<<index;


        index = readIntAttribute(node, "corner_index3", &bOk);
        if (true != bOk)
        {
            qDebug()<<"cornerid3 read error";
            return listId;
        }

        listId<<index;

        return listId;
    }

    void VAOneXMLAdaptorSeaFacesSingleCurve::readCell(rapidxml::xml_node<char> *node, Acoustics::FITKAbstractSEAFace *data)
    {
        if ((nullptr == node) || (nullptr == data))
        {
            return;
        }

        // <FlatPlateFace face_id="1">
        //         <NodeRef nodeid="13"/>
        //         <NodeRef nodeid="14"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <PositiveFace trim_ref="Multiple NCT" trim_coverage="0.99"/>
        //         <NegativeFace trim_ref="2-layer treatment" trim_coverage="0.88"/>
        //</FlatPlateFace>

        // <SinglyCurvedFace face_id="7" face_name="" corner_index1="14" corner_index2="67" corner_index3="81">
        //         <NodeRef nodeid="13"/>
        //         <NodeRef nodeid="14"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <PositiveFace trim_ref="Multiple NCT" trim_coverage="0.99"/>
        //         <NegativeFace trim_ref="2-layer treatment" trim_coverage="0.88"/>
        //</SinglyCurvedFace>

        // <DoublyCurvedShellFace face_id="15" face_name="" number_of_rings="1" tip_nodeid="16920" nodes_per_ring="198">
        //         <NodeRef nodeid="13"/>
        //         <NodeRef nodeid="14"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <Triangle node_id1="13" node_id2="154" node_id3="59"/>
        //         <PositiveFace trim_ref="Multiple NCT" trim_coverage="0.99"/>
        //         <NegativeFace trim_ref="2-layer treatment" trim_coverage="0.88"/>
        //</DoublyCurvedShellFace>

        readFaceId(node, data);

        //读取点信息
        QList<Acoustics::FITKAbstractSEASubSysWithNodes::SubSysNode> listNode = readNode(node);
        QList<int> listCornerIndex = readCornerList(node, data);

        for (int i = 0; i < listNode.size(); i++)
        {
            if (listCornerIndex.contains(i))  //单曲corner节点
            {
                listNode[i]._type = Acoustics::FITKAbstractSEASubSysWithNodes::Corner;
            }
        }

        //起始节点类型设置
        if (2 <= listNode.size())
        {
            listNode.first()._type = Acoustics::FITKAbstractSEASubSysWithNodes::Start;
            listNode.last()._type = Acoustics::FITKAbstractSEASubSysWithNodes::Finish;
        }

        data->setNodes(listNode);

        readTriangle(node, data);

        readNCT(node, data);
    }

    void VAOneXMLAdaptorSeaFacesSingleCurve::writeCornerList(Acoustics::FITKAbstractSEAFace *data, rapidxml::xml_node<char> *node)
    {
        if ((nullptr == node) || (nullptr == data))
        {
            return;
        }

        //单曲率面有corner参数
        QList<int> listIndex;
        QList<Acoustics::FITKAbstractSEASubSysWithNodes::SubSysNode> listNode = data->getNodes();
        int i = 0;
        for (auto key :listNode)
        {
            if (Acoustics::FITKAbstractSEASubSysWithNodes::Corner == key._type)
            {
                listIndex<<i;
            }
            i++;
        }

        if (3 != listIndex.size())
        {
            return;
        }

        writeAttribute(node, "corner_index1", listIndex[0]);
        writeAttribute(node, "corner_index2", listIndex[1]);
        writeAttribute(node, "corner_index3", listIndex[2]);
    }

    void VAOneXMLAdaptorSeaFacesSingleCurve::writeCell(Acoustics::FITKAbstractSEAFace *data, rapidxml::xml_node<char> *node)
    {
        if ((nullptr == data) || (nullptr == node))
        {
            return;
        }

        //使用objid作为face id
        writeFaceId(data, node);

        writeNode(data, node);

        writeCornerList(data, node);

        //写入三角单元
        writeTriangle(data, node);

        //噪声信息
        writeNCT(data, node);
    }
}
